﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using Telerik.Web.UI;
using DTO;
using BUS;


namespace TCV_WEB.Admin.Page
{
    public partial class Orderpage : System.Web.UI.Page
    {
        private int iNewcatid = -1;
        private String sErr = "";
        private String _sErr = "";


        protected void Page_Load(object sender, EventArgs e)
        {
        }


        private void LoadDataGrid()
        {

            try
            {

                BUS.OrderControl orderctr = new BUS.OrderControl();

                DataTable dt = orderctr.Get_ByUserID(-1, ref sErr);


                RadGrid1.DataSource = dt;
            }
            catch (Exception ex)
            {
                lblErr.Text = ex.Message;
            }
        }

        protected void btAddnew_Click(object sender, EventArgs e)
        {
            Session["productid"] = null;
            Response.Redirect("~/Admin/Page/DetailProduct.aspx");
        }

        protected void RadGrid1_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            LoadDataGrid();
        }

        protected void RadGrid1_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
        {
            if (e.CommandName.ToUpper() == "USERID")
            {
                try
                {
                    int inewsid = Convert.ToInt32(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["ID"].ToString().Trim());
                    OrderControl prd_control = new OrderControl();
                    DTO.OrderInfo odInfo = prd_control.Get(inewsid, ref sErr);
                    Session["userid"] = odInfo.UserID;
                    Response.Redirect("~/Admin/Page/OrderUsers.aspx");
                }
                catch (System.Exception ex)
                {
                    lblErr.Text = ex.Message;
                }
            }
            else if (e.CommandName.ToUpper() == "LOAD")
            {
                int inewsid = Convert.ToInt32(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["ID"].ToString().Trim());

                Session["orderid"] = inewsid;
                Response.Redirect("~/Admin/Page/DetailOrder.aspx");
            }
            else if (e.CommandName.ToUpper() == "DELETE")
            {
                int inewsid = Convert.ToInt32(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["ID"].ToString().Trim());
                OrderControl prd_control = new OrderControl();
                sErr = prd_control.Delete(inewsid);

                RadGrid1.Rebind();
            }
        }

        protected void radgridOrder_DetailTableDataBind(object sender, GridDetailTableDataBindEventArgs e)
        {
            GridDataItem dataitem = (GridDataItem)e.DetailTableView.ParentItem;
            if (e.DetailTableView.Name == "Orders")
            {
                String orderprd = dataitem.GetDataKeyValue("OrderProducts").ToString();

                BUS.OrderProductControl ctr = new BUS.OrderProductControl();
                DataTable dt = ctr.GetByOrder(orderprd, ref sErr);

                e.DetailTableView.DataSource = dt;
            }
        }

        protected void RadGrid1_DeleteCommand(object sender, GridCommandEventArgs e)
        {
            string ID = (e.Item as GridDataItem).OwnerTableView.DataKeyValues[e.Item.ItemIndex]["ID"].ToString();

            OrderControl ctr = new OrderControl();
            sErr = ctr.Delete(Convert.ToInt32(ID));

            if (sErr != "")
                RadGrid1.Controls.Add(new LiteralControl("Unable to detele cause" + sErr));
            else
                RadGrid1.Controls.Add(new LiteralControl("Delete successfully !!!"));
        }

        protected void RadGrid1_ItemCommand1(object sender, GridCommandEventArgs e)
        {

        }

        protected void RadGrid1_UpdateCommand(object sender, GridCommandEventArgs e)
        {
            GridEditableItem editedItem = e.Item as GridEditableItem;
            GridEditManager editMan = editedItem.EditManager;

            foreach (GridColumn column in e.Item.OwnerTableView.RenderColumns)
            {
                if (column is IGridEditableColumn)
                {
                    IGridEditableColumn editableCol = (column as IGridEditableColumn);
                    if (editableCol.IsEditable)
                    {
                        IGridColumnEditor editor = editMan.GetColumnEditor(editableCol);

                        string editorType = editor.ToString();
                        string editorText = "unknown";
                        object editorValue = null;

                        if (editor is GridTextColumnEditor)
                        {
                            editorText = (editor as GridTextColumnEditor).Text;
                            editorValue = (editor as GridTextColumnEditor).Text;
                        }

                        if (editor is GridBoolColumnEditor)
                        {
                            editorText = (editor as GridBoolColumnEditor).Value.ToString();
                            editorValue = (editor as GridBoolColumnEditor).Value;
                        }

                        if (editor is GridTemplateColumnEditor)
                        {
                            RadComboBox combo = (RadComboBox)editedItem.FindControl("radcbbStatus");
                            editorText = combo.Text;
                            editorValue = combo.SelectedValue;
                        }
                        if (editor is GridDateTimeColumnEditor)
                        {
                            editorText = (editor as GridDateTimeColumnEditor).Text;
                            editorValue = (editor as GridDateTimeColumnEditor).Text;
                        }

                        try
                        {
                            OrderControl orcrt = new OrderControl();
                            OrderInfo inf = orcrt.Get(Convert.ToInt32(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["ID"].ToString()), ref sErr);
                            DataTable dt = OrderInfo.ToDataTable();
                            DataRow row = inf.ToDataRow(dt);
                            row[column.UniqueName] = editorValue;
                            inf = new OrderInfo(row);
                            orcrt.Update(inf);
                            //changedRows[0][column.UniqueName] = editorValue;
                            //this.EmployeesData.Tables["Employees"].AcceptChanges();
                        }
                        catch (Exception ex)
                        {
                            RadGrid1.Controls.Add(new LiteralControl("<strong>Unable to set value of column '" + column.UniqueName + "'</strong> - " + ex.Message));
                            e.Canceled = true;
                            break;
                        }

                    }
                }
            }
        }

        protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item.IsInEditMode)
            {
                GridEditableItem item = (GridEditableItem)e.Item;
                if (!(e.Item is IGridInsertItem))
                {
                    RadComboBox combo = (RadComboBox)item.FindControl("radcbbStatus");
                    StatusControl ctr = new StatusControl();
                    DataTable dt = ctr.GetAll(ref sErr);
                    combo.DataSource = dt;
                    combo.DataTextField = "NameVN";
                    combo.DataValueField = "ID";

                    combo.SelectedValue = e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["IDStatus"].ToString();
                    combo.DataBind();
                }

            }
        }


    }
}
